package cnpm.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import cnpm.dao.EmployeeDAO;
import cnpm.entity.Bank;
import cnpm.entity.Department;
import cnpm.entity.Employee;
import cnpm.entity.Office;
import cnpm.util.HibernateUtil;

public class EmployeeDAOImpl implements EmployeeDAO{

	private SessionFactory sessionFactory;
	private Session session;
	
	public EmployeeDAOImpl() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> getAllEmployee() {
		List<Employee> list = new ArrayList<Employee>();
		try{
			session = sessionFactory.openSession();
			list = (List<Employee>)session.createCriteria(Employee.class)
					.addOrder(Order.asc("firstName"))
					.list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}

	@Override
	public Employee getEmployeeByID(String empID) {
		try{
			session = sessionFactory.openSession();
			return (Employee) session.get(Employee.class, empID);
		}catch(HibernateException e){
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return null;
	}

	@Override
	public boolean saveOrUpdateEmployee(Employee e) {
		try{
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.saveOrUpdate(e);
			session.beginTransaction().commit();
			return true;
		}catch(HibernateException e1){
			if(session.beginTransaction() == null){
				session.beginTransaction().rollback();
			}
			e1.printStackTrace();
		}finally{
			session.close();
		}
		return false;
	}

	@Override
	public boolean deleteEmployee(Employee e) {
		try{
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.delete(e);
			session.beginTransaction().commit();
			return true;
		}catch(HibernateException e1){
			if(session.beginTransaction() == null){
				session.beginTransaction().rollback();
			}
			e1.printStackTrace();
		}finally{
			session.close();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> searchEmployee(String empFirstName,
			String empLastName, String empPhone, Office empOffice,
			String empEmail, Department empDept) {
		List<Employee> list = new ArrayList<Employee>();
		try{
			session = sessionFactory.openSession();
			Criteria cri = session.createCriteria(Employee.class);
			if(!empFirstName.equals("")){
				cri = cri.add(Restrictions.ilike("firstName", "%"+empFirstName+"%"));
			}
			if(!empLastName.equals("")){
				cri = cri.add(Restrictions.ilike("lastName", "%"+empLastName+"%"));
			}
			if(!empPhone.equals("")){
				cri = cri.add(Restrictions.ilike("phone", "%"+empPhone+"%"));
			}
			if(!empEmail.equals("")){
				cri = cri.add(Restrictions.ilike("email", "%"+empEmail+"%"));
			}
			if(empOffice != null){
				cri = cri.add(Restrictions.eq("office", empOffice));
			}
			if(empDept != null){
				cri = cri.add(Restrictions.eq("department", empDept));
			}
			list = cri.list();
			return list;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExitEmail(String email) {
		try{
			session = sessionFactory.openSession();
			List<Employee> employees = (List<Employee>)session.createCriteria(Employee.class)
					.add(Restrictions.eq("email", email))
					.list();
			if(employees.size() > 0){
				return true;
			}
		}catch(HibernateException ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExitIndentityCard(String indentityCard) {
		try{
			session = sessionFactory.openSession();
			List<Employee> employees = (List<Employee>)session.createCriteria(Employee.class)
					.add(Restrictions.eq("indentityCard", indentityCard))
					.list();
			if(employees.size() > 0){
				return true;
			}
		}catch(HibernateException ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExitPhone(String phone) {
		try{
			session = sessionFactory.openSession();
			List<Employee> employees = (List<Employee>)session.createCriteria(Employee.class)
					.add(Restrictions.eq("phone", phone))
					.list();
			if(employees.size() > 0){
				return true;
			}
		}catch(HibernateException ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}

		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExitAccountBank(String accountBank, Bank bank) {
		try{
			session = sessionFactory.openSession();
			List<Employee> employees = (List<Employee>)session.createCriteria(Employee.class)
					.add(Restrictions.eq("accountBank", accountBank))
					.add(Restrictions.eq("bank", bank))
					.list();
			if(employees.size() > 0){
				return true;
			}
		}catch(HibernateException ex){
			ex.printStackTrace();
		}finally{
			session.close();
		}

		return false;
	}

}
